package trackClustering.example.reader;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import org.locationtech.proj4j.BasicCoordinateTransform;
import org.locationtech.proj4j.CRSFactory;
import org.locationtech.proj4j.CoordinateReferenceSystem;
import org.locationtech.proj4j.ProjCoordinate;
import trackClustering.example.entity.TrajectoryPoint;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TrajectoryReader {

    public static List<TrajectoryPoint> readTrajectoryData(String filePath) throws IOException, CsvValidationException {
        CRSFactory csFactory = new CRSFactory();
        CoordinateReferenceSystem wgs84 = csFactory.createFromName("EPSG:4326"); // WGS84
        CoordinateReferenceSystem utm50n = csFactory.createFromName("EPSG:32650"); // UTM Zone 50N

        List<TrajectoryPoint> points = new ArrayList<>();
        try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
            String[] header = reader.readNext(); // Skip header row
            String[] line;
            while ((line = reader.readNext()) != null) {
                if (line.length >= 24) { // Ensure all fields are present
                    double latitude = convertToDouble(line[9]);
                    double longitude = convertToDouble(line[10]);
                    ProjCoordinate fromCoord = new ProjCoordinate(longitude, latitude);
                    ProjCoordinate toCoord = new ProjCoordinate();
                    // 创建坐标转换对象
                    BasicCoordinateTransform transform = new BasicCoordinateTransform(wgs84, utm50n);
                    // 执行转换
                    transform.transform(fromCoord, toCoord);
                    TrajectoryPoint point = new TrajectoryPoint(
                            line[0], line[1], line[2], line[3], line[4],
                            convertToDouble(line[5]), convertToDouble(line[6]),
                            convertToDouble(line[7]), convertToDouble(line[8]),
                            convertToDouble(line[9]), convertToDouble(line[10]),
                            convertToDouble(line[11]), convertToDouble(line[12]),
                            line[13], convertToDouble(line[14]), convertToDouble(line[15]),
                            convertToDouble(line[16]), line[17], convertToDouble(line[18]),
                            convertToDouble(line[19]), convertToDouble(line[20]),
                            convertToInteger(line[21]), line[22], convertToDouble(line[23]),
                            toCoord.x, toCoord.y // 新增UTM坐标
                    );
                    points.add(point);
                }
            }
        }
        return points;
    }

    private static Double convertToDouble(String value) {
        if (value == null || value.equalsIgnoreCase("NULL") || value.isEmpty()) {
            return null;
        }
        try {
            return Double.parseDouble(value);
        } catch (NumberFormatException e) {
            // 可以选择记录日志或抛出自定义异常
            System.err.println("Invalid double value: " + value);
            return null;
        }
    }

    private static Integer convertToInteger(String value) {
        if (value == null || value.equalsIgnoreCase("NULL") || value.isEmpty()) {
            return null;
        }
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            // 可以选择记录日志或抛出自定义异常
            System.err.println("Invalid integer value: " + value);
            return null;
        }
    }

}